package com.huchao.www.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @Description: 学生状态-转换器
 * @BelongsProject: background-management-v10
 * @BelongsPackage: com.huchao.www.utils
 * @Author: 亖思
 * @CreateTime: 2023/3/16 11:01
 * @Version: 1.0
 */
public class GenderConverter implements Converter<Integer> {

    private static final String atSschool = "在校";
    private static final String leaveSchool = "离校";

    @Override
    public Class<?> supportJavaTypeKey() {
        // 实体类中对象属性类型
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        // Excel中对应的CellData属性类型
        return CellDataTypeEnum.STRING;
    }


    /**
     * 读的时候调用
     *
     * @param context
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        // 从Cell中读取数据
        String state = context.getReadCellData().getStringValue();
        // 判断Excel中的值，将其转换为预期的数值
        if (atSschool.equals(state)) {
            return 1;
        } else if (leaveSchool.equals(state)) {
            return 0;
        }
        return null;
    }

    /**
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<>(value.equals(1) ? atSschool : leaveSchool);
    }
}


